var app = getApp()
const quote = app.globalData.quote;
const api = quote.api;
const tool = quote.tool;
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //   animation: null,//引导页动画
        isShowGuidance: true,//是否显示引导页
        taskStatus: app.globalData.taskStatus,//任务状态
        imgUrl: app.globalData.imgUrl,//图片地址
        isBrang: app.globalData.isBrang,//苹果系列的刘海屏手机
        taskTwoData: null,
        taskTwoAllData: null,
        isTrue: false,//判断用户是否选对答案
        isShowTip: false,//是否通关完毕
        //   isShowEndTip: false,//任务结束弹窗
        isShowTrue: false,//是否显示对的图标
        isListenEnd: false,//是否听完录音次才可以重新点击听声音
        isClickContinue: false//防止连续点击两次   
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        that.endTip = this.selectComponent("#endTip");
        that.setData({
            taskStatus: app.globalData.taskStatus,//任务状态
        })
        that.getTaskTwoData()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this
        innerAudioContext.onPlay((res) => {
            console.log('音频开始播放任务二', res)
            that.setData({
                isListenEnd: false
            })
        })

        innerAudioContext.onEnded((res) => {
            console.log('音频结束播放任务二', res)
            that.setData({
                isListenEnd: true
            })

        })


    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    beginTask: function () {
        var that = this
        that.setData({
            isShowGuidance: false,
        })
        innerAudioContext.src = that.data.taskTwoAllData[0].AudioAddress
        innerAudioContext.play()
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            path: 'pages/indexPackage/index/index',
            success: function (res) {
                console.log('转发成功', res);
            },
            fail: function (err) {
                console.log('转发失败', err)
            }
        }
    },

    //结束任务
    endTask: function () {
        var that = this
        that.endTip.showPopup();
    },
    //用户点击取消
    cancal: function () {
        var self = this
        self.endTip.hidePopup();
    },
    //用户点击确定
    confirm: function () {
        var that = this
        tool.statistics(app.globalData.openId, app.globalData.moduleId, 3, 2, 1)
        innerAudioContext.offPlay()
        innerAudioContext.offEnded()
        wx.redirectTo({
            url: '../../../../pages/indexPackage/courseList/courseList',
        })
    },
    //获取任务二的数据
    getTaskTwoData: function () {
        var dataDic = {
            ModuleId: app.globalData.moduleId,
            TaskMark: 2
        };
        var that = this
        api.methodBasicEnglish_GetTaskData({
            dataDic: dataDic,
            complete: function (res) {
                console.log(res.data.data)
                var data = res.data.data.TaskList
                data.forEach(item => {
                    item.AudioAddress = app.globalData.audioUrl + item.AudioAddress
                    item.OptionList.forEach(item1 => {
                        item1.Url = app.globalData.imgUrl + item1.Url
                    })
                })
                that.setData({
                    taskTwoData: data[0],
                    taskTwoAllData: data
                })
               console.log('数据+++++', that.data.taskTwoAllData, app.globalData.audioUrl + res.data.data.TaskList[0].AudioAddress)
            }
        })
    },
    //选择答案
    selectAnswer: function (e) {
        var that = this
        if (that.data.isClickContinue) {
            console.log('防止连续点击')
            return
        }
        that.setData({
            isShowTrue: true,
            isClickContinue: true
        })
        console.log(e, that.data.taskTwoData)
        var index = e.currentTarget.dataset.index
        var arr = 'taskTwoData.OptionList[' + index + '].Status'
        if (index == that.data.taskTwoData.Answer) {
            //选对答案
            innerAudioContext.src = '/src/record/true1.mp3'
            innerAudioContext.play()
            console.log('right')
            that.setData({
                isTrue: true,
                [arr]: !that.data.taskTwoData.OptionList[0].Status
            })
            that.data.taskTwoAllData.splice(0, 1)
            if (that.data.taskTwoAllData.length > 0) {
                console.log('还没测完')
                setTimeout(() => {
                    that.setData({
                        isShowTrue: false,
                        taskTwoData: that.data.taskTwoAllData[0],
                        isClickContinue: false
                    })
                    //播放下一道题目的音频
                    // innerAudioContext.src = app.globalData.audioUrl + that.data.taskTwoAllData[0].AudioAddress
                    innerAudioContext.src = that.data.taskTwoAllData[0].AudioAddress
                    innerAudioContext.play()
                }, 1500)
            } else {
                console.log('测完')
                setTimeout(() => {
                    that.setData({
                        isShowTip: true,
                        isClickContinue: false
                    })
                }, 1500)
            }

        } else {
            //选错答案
            console.log('err', that.data.taskTwoAllData)
            innerAudioContext.src = '/src/record/false.mp3'
            innerAudioContext.play()
            that.setData({
                isTrue: false,
                [arr]: !that.data.taskTwoData.OptionList[0].Status
            })
            setTimeout(() => {
                //将点击过的状态全部复原
                for (var j = 0; j < that.data.taskTwoData.OptionList.length; j++) {
                    if (that.data.taskTwoData.OptionList[j].Status) {
                        var arr = 'taskTwoData.OptionList[' + j + '].Status'
                        that.setData({
                            [arr]: false
                        })
                    }
                }
                that.data.taskTwoAllData.splice(0, 1)
                that.data.taskTwoAllData.push(that.data.taskTwoData)
                console.log(that.data.taskTwoAllData)
                that.setData({
                    isShowTrue: false,
                    isClickContinue: false,
                    taskTwoData: that.data.taskTwoAllData[0]
                })
                //播放下一道题目的音频
                if (that.data.taskTwoAllData.length > 1) {
                    console.log('不是最后一道题')
                    // innerAudioContext.src = app.globalData.audioUrl + that.data.taskTwoAllData[0].AudioAddress
                    innerAudioContext.src = that.data.taskTwoAllData[0].AudioAddress
                    innerAudioContext.play()
                }

            }, 1500)

        }
    },
    //点击播放录音按钮
    listenSound: function (e) {
        var that = this
        console.log(e)
        var src = e.currentTarget.dataset.listensrc
        that.setData({
            isListenEnd: false
        })
        // innerAudioContext.src = app.globalData.audioUrl + src
        innerAudioContext.src = src
        innerAudioContext.play()

    },
    //通关
    nextOne: function () {
        var that = this
        console.log(that.data.taskStatus,'262')
        innerAudioContext.offPlay()
        innerAudioContext.offEnded()
        if (that.data.taskStatus.TaskThree == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskThree/taskThree',
            })
            return
        } else if (that.data.taskStatus.TaskFour == true) {
            wx.navigateTo({
                url: '/pages/taskPackage/pages/taskFour/taskFour',
            })
            return
        } else {

        }
    },

})